<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\admin\RolePermissionRequest;
use App\Http\Requests\Admin\RoleRequest;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	$roleList = Role::query()
					->orderBy('id','desc')
					->paginate(10);

        return view('admin.role.index',compact('roleList'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RoleRequest $request,Role $roleModel)
    {
		if($request->method() == 'POST'){
			$insertData = $request->except('_token');

			$roleModel->fill($insertData);
			$roleModel->save();

			return redirect()->route('admin.role.index')->with('success','添加成功');
		}
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
		$role = Role::findOrFail($id);

        return view('admin.role.edit',compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(RoleRequest $request, $id)
    {
        if($request->method() == 'POST'){

        	$role = Role::findOrFail($id);

         	$updateData = $request->except('_token');

         	$role->fill($updateData);
         	$role->update();

			return redirect()->route('admin.role.index')->with('success','修改成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $role = Role::findOrFail($id);
        $role->delete();

		//删除用户和角色的关系链
		$role->role_admin()->update([
			'admin_role.deleted_at'=>date('Y-m-d H:i:s',time())
		]);
		//删除角色和权限的关系链
//		$role->role_permission()->detach();

		return redirect()->route('admin.role.index')->with('success','删除成功');
    }

	/**
	 * 编辑权限
	 * User: ihome
	 * @param $id
	 */
    public function editPermission($id)
    {
    	$role = Role::findOrFail($id);

    	//取出权限列表
		$permissions = Permission::query()->select('id','permission_name','pid')->get()->toArray();

		$permissionList = [];
		
		foreach ($permissions as $k => $v) {
		    if($v['pid'] == 0){
		        foreach ($permissions as $k1 => $v1) {
		            if($v1['pid'] == $v['id']){
		            	foreach ($permissions as $k2 => $v2) {
		            	    if($v2['pid'] == $v1['id']){
								foreach ($permissions as $k3 => $v3) {
									if($v3['pid'] == $v2['id']){
										$v2['child'][] = $v3;
									}
								}
								$v1['child'][] = $v2;
		            	    }
		            	}
						$v['child'][] = $v1;
		            }
		        }
				$permissionList[] = $v;
		    }
		}

		//把已有权限取出来
		$permissionIds = $role->role_permission()->select('permission_id')->get()->toArray();
		$permissionIds = array_column($permissionIds,'permission_id');

        return view('admin.role.edit_permission',compact('role','permissionList','permissionIds'));
    }

	/**
	 * 权限更新
	 * User: ihome
	 * @param $id
	 */
	public function updatePermission(RolePermissionRequest $request,$id)
	{
		if($request->method() == 'POST'){
			$role = Role::findOrFail($id);

			$rolePermissionData = $request->except('_token');

//			$role->role_permission()->detach();
			$role->role_permission()->sync($rolePermissionData['permission_id']);

//			$role = Role::findOrFail($id);
			//查询所有权限列表
//			$role->updatePermissionCache([$id]);

			return back()->with('success','更新成功');
		}
	}
}
